<?php
require_once ('..' . DIRECTORY_SEPARATOR . 'initialize.php');
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    ?>
    <div class='rating'>
        <input type="hidden" id="rate-quality" value="<?php echo $solution->getQuality(); ?>"/>
        <div id="rate-<?php echo $solution->getId(); ?>" class="rate_widget">
            <div id="rate-<?php echo $solution->getId(); ?>_star_1" class="star_1 ratings_stars"></div>
            <div id="rate-<?php echo $solution->getId(); ?>_star_2" class="star_2 ratings_stars"></div>
            <div id="rate-<?php echo $solution->getId(); ?>_star_3" class="star_3 ratings_stars"></div>
        </div>
    </div>
    <script type="text/javascript" src="<?php echo Utils::getUrl('scripts/solution.js') ?>"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var id = $(".rate_widget").attr("id").split('-')[1];
            var quality = $("#rate-quality").val();
            set_vote($("#rate-" + id), quality);    
            $('.ratings_stars').hover(
            function() {
                star_hover_in(this);
            },
            function() {
                star_hover_out(this);
            });
            $('.ratings_stars').click(function() {
                star_clicked(this);
            });
        });
    </script>
    <?php
} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    include_once(Utils::getPath(array('repositories', 'solutionRepository.php')));
    include_once(Utils::getPath(array('repositories', 'challengeRepository.php')));

    //Get Entities from Repositories
    $solutionRep = new SolutionRepository();
    $solution = $solutionRep->getById($_POST['solution']);

    $challengeRep = new ChallengeRepository();
    $challenge = $challengeRep->getById($solution->getIdChallenge());

    if ($solution == null || $challenge == null) {
        Utils::goToErrorPage(StatusCodes::HTTP_BAD_REQUEST);
    }
    //Check permisions, only challenge author can rate a solution
    if ($challenge->getIdAuthor() == $_SESSION['authenticatedId']) {
        if (isset($_POST['rate']) && !isset($_POST['valid'])) {
            $solution->setQuality($_POST['rate']);
            $solution = $solutionRep->persist($solution);

            $data = array();
            $data['result'] = $solution->getQuality();
            echo json_encode($data);
            exit();
        } else if (isset($_POST['valid']) && !isset($_POST['rate'])) {
            if ($_POST['valid'] == "true")
                $valid = true;
            else if ($_POST['valid'] == "false")
                $valid = false;
            else
                $valid = null;

            $solution->setValid($valid);
            $solution->setQuality(1);
            $solution = $solutionRep->persist($solution);

            $data = array();
            $data['result'] = $solution->getValid();
            echo json_encode($data);
            exit();
        } else {
            Utils::goToErrorPage(StatusCodes::HTTP_BAD_REQUEST);
        }
    } else {
        Utils::goToErrorPage(StatusCodes::HTTP_FORBIDDEN);
    }
}
?>